package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.PermissionService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName PermissionController
 * @Description
 * @Author Mr.Ho
 * @Date 10:10 2019/7/16
 * @Version 1.1
 **/

@RestController
@RequestMapping("/permission")
public class PermissionController {


    @Reference
    private PermissionService permissionService;

    //分页查询记录
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Permission> pageList = null;
        try {
            pageList = permissionService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,pageList);
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    //回显编辑数据
    @RequestMapping("/findById")
    public Result findById(int id){
        Permission permission = null;
        try {
            permission = permissionService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permission);
    }

    //修改数据
    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    //根据id删除记录
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        permissionService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    //查询所有列表
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Permission> permissionList = permissionService.findAll();
        if(permissionList!=null&&permissionList.size()!=0)
        {
            Result result = new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionList);
            return result;
        }else {
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    //根据role的id查找
    @RequestMapping("/findPermissionIdsByRoleId")
    public List<Integer> findPermissionIdsByRoleId(int id){
        List<Integer> list = permissionService.findPermissionIdsByRoleId(id);
        return list;
    }
}
